#!/bin/sh

#输出
BASE_OUT_DIR="/out/http_client"

DEMO_SWAGGER_JSON="demo"



generate_http_api(){
  # $1 OUT_DIR
  # $2 PB_DIR
  # $3 prefix
  # $4 api_package

  for json_file in `ls $2 | grep ".json"`
  do
    json_path=$2"/"$json_file

    config_param=""
    if [ -n "$3" ];then
      config_param="--model-name-prefix $3"
    fi
    if [ -n "$4" ];then
      config_param="$config_param --api-package=$4"
    fi

    java -jar /opt/swagger-codegen-cli/swagger-codegen-cli.jar generate \
         -i $json_path \
         -l python \
         -o $1 \
         $config_param \
         -DapiTests=false,apiDocs=false,modelTests=false,modelDocs=false
  done

  # rm unnecessary files
  for tmp_file in `ls -A $1 | grep -v swagger_client`
  do
    rm -rf $1"/"$tmp_file
  done
}

case $1 in
"all")
    # generate all http interface code
    rm -rf $BASE_OUT_DIR
    generate_http_api $BASE_OUT_DIR $DEMO_SWAGGER_JSON
    ;;
"demo")
    # update demo http interface code
    generate_http_api $BASE_OUT_DIR $DEMO_SWAGGER_JSON
    ;;
*)
    # doesn't support
    echo "Doesn't support $1 parameter"
    exit
    ;;
esac
